local bazhan = fk.CreateSkill{
  name = "mod__bazhan",
}
local bazhan_ = {
  anim_type = "switch",
  switch_skill_name = "mod__bazhan",
  prompt = function (self, player)
    return player:getSwitchSkillState("mod__bazhan", false) == fk.SwitchYang and "#mod__bazhan-Yang" or "#mod__bazhan-Yin"
  end,
  target_num = 1,
  max_card_num = function (self, player)
    return (Self:getSwitchSkillState("mod__bazhan", false) == fk.SwitchYang) and 2 or 0
  end,
  min_card_num = function (self, player)
    return (Self:getSwitchSkillState("mod__bazhan", false) == fk.SwitchYang) and 1 or 0
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) < 1
  end,
  card_filter = function(self, player, to_select, selected)
    return #selected < self:getMaxCardNum() and Fk:currentRoom():getCardArea(to_select) ~= Player.Equip
  end,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected_cards >= self:getMinCardNum() and #selected == 0 and to_select ~= player
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local isYang = player:getSwitchSkillState(self.name, true) == fk.SwitchYang

    local to_check = {}
    if isYang and #effect.cards > 0 then
      table.insertTable(to_check, effect.cards)
      local dummy = Fk:cloneCard("dilu")
      dummy:addSubcards(to_check)
      room:obtainCard(target.id, dummy, false, fk.ReasonGive, player.id)
    elseif not isYang and not target:isKongcheng() then
      to_check = room:askForCardsChosen(player, target, 1, 1, "h", self.name)
      local dummy = Fk:cloneCard("dilu")
      dummy:addSubcards(to_check)
      room:obtainCard(player, dummy, false, fk.ReasonPrey)
      target = player
    end
    if not player.dead and not target.dead and table.find(to_check, function (id)
    return Fk:getCardById(id).name == "analeptic" or Fk:getCardById(id).suit == Card.Heart end) then
      local choices = {"cancel"}
      if not target.faceup or target.chained then
        table.insert(choices, 1, "mod__bazhan_reset")
      end
      if target:isWounded() then
        table.insert(choices, 1, "recover")
      end
      if #choices > 1 then
        local choice = room:askForChoice(player, choices, self.name, "#mod__bazhan-support::" .. target.id)
        if choice == "recover" then
          room:recover{ who = target, num = 1, recoverBy = player, skillName = self.name }
        elseif choice == "mod__bazhan_reset" then
          if not target.faceup then
            target:turnOver()
          end
          if target.chained then
            target:setChainState(false)
          end
        end
      end
    end
  end,
}
bazhan:addEffect("active", bazhan_)
return bazhan